package com.jsbj.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.jsbj.base.ResultBean;
import com.jsbj.framework.controller.BaseController;
import com.jsbj.page.PageReq;
import com.jsbj.user.entities.SysTenant;
import com.jsbj.user.entities.SysUser;
import com.jsbj.user.service.ISysTenantService;
import com.jsbj.user.service.ISysUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 系统租户表 前端控制器
 * </p>
 *
 * @author jsbj
 * @since 2022-02-22
 */
@RestController
@RequestMapping("/sys-tenant")
@Api(value = "租户管理", tags = "租户管理")
@ApiIgnore
public class SysTenantController extends BaseController {

    @Autowired
    private ISysTenantService sysTenantService;

    @Autowired
    private ISysUserService sysUserService;

    @ApiIgnore
    @ApiOperation("根据用户登录id查询所有租户")
    @ApiImplicitParam(name = "userId", value = "用户登录id", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenant.class)})
    @PostMapping("/getByUserId")
    public ResultBean<List<SysTenant>> getByUserId(String userId) {
        logger.error("intogetByUserId----------------------");
        return new ResultBean<List<SysTenant>>(sysTenantService.getByUserId(userId));
    }

    @ApiIgnore
    @ApiOperation("根据租户类型查询租户信息")
    @ApiImplicitParam(name = "tenantTypeValue", value = "用户登录id", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenant.class)})
    @PostMapping("/api/getByTenantTypeValue")
    public ResultBean<List<SysTenant>> getByTenantTypeValue(String tenantTypeValue) {
        return new ResultBean<List<SysTenant>>(sysTenantService.getByTenantTypeValue(tenantTypeValue));
    }

    @ApiOperation("查询列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "页码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页条数", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenant.class)})
    @PostMapping("/getListData")
    public ResultBean<PageReq> getListData(@RequestBody String inParam) {
        return new ResultBean<PageReq>(getPage(sysTenantService.getListData(inParam)));
    }

    @ApiOperation("新增或编辑保存")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @PostMapping("saveOrUpdateData")
    public ResultBean<JSONObject> saveOrUpdateData(@RequestBody SysTenant sysTenant){
        return sysTenantService.saveOrUpdateData(sysTenant);
    }

    @ApiOperation("查看详情")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "id（行）", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenant.class)})
    @PostMapping("getDetail")
    public ResultBean<SysTenant> getDetail(@RequestParam String id){
        return new ResultBean<SysTenant>(sysTenantService.getDetail(id));
    }

    @ApiOperation("删除数据")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "id（行）", required = true)}
    )
    @PostMapping("deleteData")
    public ResultBean<JSONObject> deleteData(@RequestParam String id){
        return sysTenantService.deleteData(id);
    }

    @ApiOperation("查询超管账户下拉框")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysUser.class)})
    @PostMapping("/getUserData")
    public ResultBean<List<SysUser>> getUserData(@RequestParam(required = false) String userName) {
        return new ResultBean<List<SysUser>>(sysUserService.getUserData(userName));
    }

    @PostMapping("/getTenantInfoMul")
    public List<SysTenant> getTenantInfoMul(@RequestParam String tenantCodeLs) {
        return sysTenantService.getTenantInfoMul(Arrays.asList(tenantCodeLs.split(",")));
    }

    @GetMapping("/getByUserIdPage")
    public ResultBean<List<SysTenant>> getByUserIdPage(String userId) {
        return new ResultBean<List<SysTenant>>(sysTenantService.getByUserIdPage(userId));
    }

    @ApiOperation("查询项目列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "页码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页条数", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysTenant.class)})
    @PostMapping("/getProjectList")
    public ResultBean<PageReq> getProjectList(@RequestBody String inParam) {
        return new ResultBean<PageReq>(getPage(sysTenantService.getProjectList(inParam)));
    }

}

